package edu.ccut.saturn.component.loader.node;

/**
 * 用于解析action组件配置文件中“转移”结点的加载类。
 * <p>继承了<code>NodeInfoLoader</code>,并实现了<code>loadNode</code>方法。<p>
 * 
 * @author 温建
 * @version 1.0 2008.8.18
 * 
 * @see <code>DataTransformNodeInfo</code>
 */
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;
import org.osgi.framework.Bundle;

import edu.ccut.saturn.component.INodeInfo;
import edu.ccut.saturn.component.ITransformContent;
import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.impl.TransformContentInfo;
import edu.ccut.saturn.component.impl.node.DataTransformNodeInfo;

public class TransformNodeInfoLoader extends NodeInfoLoader {

	public static final String KEY_NODETYPE_TRANSFORM = "transform";

	public static final String KEY_TRANSFORM_KEY = "value-key";

	public static final String KEY_TRANSFORM_TYPE = "type";

	public static final String KEY_TRANSFORM_CONTENTS = "contents";

	public static final String KEY_TRANSFORM_CONTENT_TYPE = "type";
	
	public static final String KEY_TRANSFORM_CONTENT_KEY = "content-value-key";

	public static final String KEY_TRANSFORM_CONTENTS_CONTENT = "content";

	public static final String KEY_TRANSFORM_CONTENTS_PREFIX = "prefix";

	@SuppressWarnings("unchecked")
	public List<INodeInfo> loadNode(Element root, Bundle bundle)
			throws SaturnComponentException {

		List<Element> transformNodeElements = root
				.elements(KEY_NODETYPE_TRANSFORM);

		List<INodeInfo> transformNodeInfos = new ArrayList<INodeInfo>(
				transformNodeElements.size());

		for (Element transformNodeElement : transformNodeElements) {
			String id = transformNodeElement.attributeValue(KEY_NODE_ID).trim();
			String x = transformNodeElement.attributeValue(KEY_NODE_X).trim();
			String y = transformNodeElement.attributeValue(KEY_NODE_Y).trim();
			String nextId = transformNodeElement.attributeValue(KEY_NODE_NEXT)
					.trim();

			String name = transformNodeElement.elementTextTrim(KEY_NODE_NAME);
			String description = transformNodeElement
					.elementTextTrim(KEY_NODE_DESCRIPTION);

			String valueKey = transformNodeElement
					.elementTextTrim(KEY_TRANSFORM_KEY);
			String valueType = transformNodeElement
					.elementTextTrim(KEY_TRANSFORM_TYPE);
			
			List<Element> transformContents = transformNodeElement.element(
					KEY_TRANSFORM_CONTENTS).elements(
					KEY_TRANSFORM_CONTENTS_CONTENT);

			List<ITransformContent> contentInfos = new ArrayList<ITransformContent>(
					transformContents.size());

			for (Element contentElement : transformContents) {
				String type = contentElement.attributeValue(
						KEY_TRANSFORM_CONTENT_TYPE).trim();

				String contentKey = contentElement
						.elementTextTrim(KEY_TRANSFORM_CONTENT_KEY);

				String prefix = contentElement
						.elementTextTrim(KEY_TRANSFORM_CONTENTS_PREFIX);

				ITransformContent contentInfo = new TransformContentInfo(type,
						contentKey, prefix);

				contentInfos.add(contentInfo);
			}

			DataTransformNodeInfo transformNodeInfo = new DataTransformNodeInfo(
					id, name, description, nextId, contentInfos, valueKey, valueType, x, y);

			transformNodeInfos.add(transformNodeInfo);
		}
		return transformNodeInfos;
	}
}
